<?php
require_once 'includefiles.php';
require_once 'model/trade.php';
require_once 'model/users.php';
require_once 'model/admindetails.php';
include_once 'model/email.php';
include_once 'model/budget.php';



$tradeObj = new Model_TRADE();
$userObj = new Model_Users();
$adminObj = new Model_AdminDetails();
$emailObj = new Model_Email();
$budgetObj = new Model_Budget();


$getuserdetail = $userObj->chkUserNameExist($_GET['emailaddress']);
	//echo "<pre>"; print_r($_GET);die();
if($getuserdetail['user_type'] == 3 || $getuserdetail['id']=='')	
{
	$gettradedetail = $tradeObj->getTradeCategoryDetailsById($_GET['trade']);
	$getjobdetail = $tradeObj->getTradeCategoryDetailsById($_GET['jobtype']);
	$budgetdetail = $budgetObj->getBudgetDetailsById($_GET['approx_budget']);
		if($getuserdetail['email'] == $_GET['emailaddress'])
		{
			// add user post job if user exits
			$userPostArray = array();
			$userPostArray["trade_id"] 			= $_GET['trade'];
			$userPostArray["jobtype_id"]		= $_GET['jobtype'];					
			$userPostArray["job_desc"] 			= $_GET['describe_job'];
			$userPostArray['workstart'] 		= $_GET['work_to_start'];	
			$userPostArray['approx_budget']		= $_GET['approx_budget'];						
			$userPostArray['area'] 				= $_GET['postcode'];
			$userPostArray['state_id'] 			= $_GET['state_id'];			
			$userPostArray['user_id'] 			= $getuserdetail['id'];			
			$userPostArray["date_added"] 		= getCurDate();
			$userPostArray["status"] 			= 1;
			$myArray = getLatLong($_GET['postcode']);
			$userPostArray['Latitude']			= $myArray['Latitude'];
			$userPostArray['Longitude']			= $myArray['Longitude'];
			$jobID = $userObj->addUserPostJobByValue($userPostArray);
			
			//$password = getRandomNumber();
			$userDetArray = array();
			$userDetArray["first_name"] 	= $_GET['fname'];
			$userDetArray["last_name"]		= $_GET['lname'];					
			$userDetArray['phone_number'] 	= $_GET['mobno'];
			//$userDetArray['password'] 		= md5($password);	
			$userObj->editUserValueById($userDetArray,$getuserdetail['id']);
			
			
			// send email message to owner
			$email_message = $emailObj->getEmailById(8);
			$e_subject = $email_message['email_subject'];
			$message = $email_message['email_content']; 
			
			$subject = str_replace('[SITENAME]', SITENAME, $e_subject);		 
			
			$message = str_replace('[NAME]', trim($_GET['fname'])." ".trim($_GET['lname']) , $message);
			$message = str_replace('[SITENAME]', SITENAME, $message);
			$message = str_replace('[USERNAME]', trim($_GET['emailaddress']), $message);
			$message = str_replace('[PHONENUMBER]',trim($_GET['mobno']), $message);
			$message = str_replace('[EMAIL]',trim($_GET['emailaddress']), $message);
			
			$message = str_replace('[DATEADDED]',trim($userPostArray["date_added"]), $message);
			$message = str_replace('[JOBSKILL]',trim($getjobdetail['trade_category']), $message);
			$message = str_replace('[DESCRIPTION]',trim($_GET['describe_job']), $message);
			$message = str_replace('[BUDGET]',"$".trim($budgetdetail['budget_name']), $message);
			$message = str_replace('[POSTCODE]',trim($_GET['postcode']), $message);
			
			$message = str_replace('[SITE_LINK]', SITENAME, $message);
			
			$to= trim($_GET['emailaddress']); 
			$adminEmail = $adminObj->getAdminDetails();
			$from = $adminEmail;
			$fromname= SITENAME;
							 
			$emailTemplate = file_get_contents('emailtemplate.html');
			$template_msg = str_replace('[SITENAME]',SITENAME , $emailTemplate);
			$template_msg = str_replace('[LOGO]','<img src="'.SITE_URL.'/uploads/logo/'.$imageDetails.'">', $template_msg);
			$template_msg = str_replace('[MESSAGE]',$message,$template_msg); 
			
			//echo $template_msg; exit;
			if(trim($to) != '')
			@send_mail($to, $template_msg, $subject, $from, $fromname); 
			
			if($_GET['postcode']!='')
			{
				$getalluserworkinpostalcode = $userObj->getalluserworkinpostalcode($_GET['state_id'],$_GET['postcode'],$_GET['jobtype'],$_GET['trade']);								
				for($n=0;$n<count($getalluserworkinpostalcode);$n++)
				{
						// send email message to tradesman for post job
						$email_message = $emailObj->getEmailById(9);
						$e_subject = $email_message['email_subject'];
						$message = $email_message['email_content']; 
						
						$subject = str_replace('[SITENAME]', SITENAME, $e_subject);		 
						
						$message = str_replace('[TNAME]', trim($getalluserworkinpostalcode[$n]['first_name'])." ".trim($getalluserworkinpostalcode[$n]['last_name']) , $message);
						$message = str_replace('[NAME]', trim($_GET['fname'])." ".trim($_GET['lname']) , $message);
						$message = str_replace('[SITENAME]', SITENAME, $message);
						$message = str_replace('[USERNAME]', trim($_GET['fname'])." ".trim($_GET['lname']), $message);							
						$message = str_replace('[DATEADDED]',trim($userPostArray["date_added"]), $message);
						
						$message = str_replace('[TRADESKILL]',trim($gettradedetail['trade_category']), $message);
						$message = str_replace('[JOBSKILL]',trim($getjobdetail['trade_category']), $message);
						$message = str_replace('[JOBDESC]',trim($_GET['describe_job']), $message);
						
						//$activationLink = SITE_URL."/leads/".base64_encode($getalluserworkinpostalcode[$n]['id'])."/".base64_encode($jobID)."/buylead/";		 
						$activationLink = SITE_URL."/leads/".base64_encode($jobID)."/buylead/";		 
						$activationLink = '<a href="'.$activationLink.'">'.$activationLink.'</a>';		 
						$message = str_replace('[BUYTHISLEAD]',$activationLink, $message);
						$message = str_replace('[SITE_LINK]', SITENAME, $message);
						
						$to= trim($getalluserworkinpostalcode[$n]['email']); 
						$adminEmail = $adminObj->getAdminDetails();
						$from = $adminEmail;
						$fromname= SITENAME;
										 
						$emailTemplate = file_get_contents('emailtemplate.html');
						$template_msg = str_replace('[SITENAME]',SITENAME , $emailTemplate);
						$template_msg = str_replace('[LOGO]','<img src="'.SITE_URL.'/uploads/logo/'.$imageDetails.'">', $template_msg);
						$template_msg = str_replace('[MESSAGE]',$message,$template_msg); 
						//echo $template_msg; exit;
						if(trim($to) != '')
						@send_mail($to, $template_msg, $subject, $from, $fromname); 
				}
			}	
		
			 $_SESSION['getbids_id']  = $getuserdetail['id'];			 
			 if($_GET['fname']=='' || $_GET['lname'] =='')
			 {
			 	$_SESSION['first_name']      = $getuserdetail['first_name'];
				$_SESSION['last_name']       = $getuserdetail['last_name'];
			 }
			 else
			 {
				$_SESSION['first_name']      = $_GET['fname'];
				$_SESSION['last_name']       = $_GET['lname'];	 
			 }
			 $_SESSION['user_type']       = $getuserdetail['user_type'];
			 $_SESSION['email']           = $getuserdetail['email'];
			
			
			//user already exist, send an email to admin (25)
			$email_message = $emailObj->getEmailById(25);
			$e_subject = $email_message['email_subject'];
			$message = $email_message['email_content']; 
			
			$subject = str_replace('[SITENAME]', SITENAME, $e_subject);		 
			$message = str_replace('[SITENAME]', SITENAME, $message);			
			$message = str_replace('[USERNAME]', trim($_GET['emailaddress']), $message);
			
			
			$message = str_replace('[PHONENUMBER]',trim($_GET['mobno']), $message);
			$message = str_replace('[EMAIL]',trim($_GET['emailaddress']), $message);
			
			$message = str_replace('[DATEADDED]',trim($userPostArray["date_added"]), $message);
			$message = str_replace('[JOB_TYPE]',trim($getjobdetail['trade_category']), $message);
			$message = str_replace('[JOB_DESCRIPTION]',trim($_GET['describe_job']), $message);
			$message = str_replace('[BUDGET]',"$".trim($budgetdetail['budget_name']), $message);
			$message = str_replace('[POSTAL_CODE]',trim($_GET['postcode']), $message);
			
			$message = str_replace('[SITE_LINK]', SITENAME, $message);
			
			$to= $email_message['from_email']; 
			$from = $email_message['from_email']; ;
			$fromname= SITENAME;
							 
			$emailTemplate = file_get_contents('emailtemplate.html');
			$template_msg = str_replace('[SITENAME]',SITENAME , $emailTemplate);
			$template_msg = str_replace('[LOGO]','<img src="'.SITE_URL.'/uploads/logo/'.$imageDetails.'">', $template_msg);
			$template_msg = str_replace('[MESSAGE]',$message,$template_msg); 
			
			//echo $template_msg; exit;
			if(trim($to) != '')
			@send_mail($to, $template_msg, $subject, $from, $fromname); 
			
			//redirect_to_link(SITE_URL.'/register/registersuccess/');
			 //$_SESSION['msg'] = "<div class='success_msg'><span >You have sucessfully post job!</span></div>";
		}
		else
		{
				// add user if not exits
				$password = getRandomNumber();
				
				$userDetArray = array();
				$userDetArray["first_name"] 	= $_GET['fname'];
				$userDetArray["last_name"]		= $_GET['lname'];					
				$userDetArray["email"] 			= $_GET['emailaddress'];
				$userDetArray['username'] 		= $_GET['emailaddress'];
				$userDetArray['password'] 		= md5($password);
				$userDetArray['phone_number'] 	= $_GET['mobno'];					
				$userDetArray['postalcode'] 	= $_GET['postcode'];
				$userDetArray['user_type'] 		= 3;		
				$userDetArray["registered_date"] = getCurDate();
				$userDetArray["user_status"] 	= 1;
				$mydataArray = getLatLong($_GET['postcode']);
				$userDetArray['Latitude']			= $mydataArray['Latitude'];
				$userDetArray['Longitude']			= $mydataArray['Longitude'];
				$userId = $userObj->addUserByValue($userDetArray);
				
				// add user post job if user not exits
				$userPostArray = array();
				$userPostArray["trade_id"] 			= $_GET['trade'];
				$userPostArray["jobtype_id"]		= $_GET['jobtype'];					
				$userPostArray["job_desc"] 			= $_GET['describe_job'];
				$userPostArray['workstart'] 		= $_GET['work_to_start'];	
				$userPostArray['approx_budget']		= $_GET['approx_budget'];						
				$userPostArray['area'] 				= $_GET['postcode'];
				$userPostArray['user_id'] 			= $userId;		
				$userPostArray['state_id'] 			= $_GET['state_id'];					
				$userPostArray["date_added"] 		= getCurDate();
				$userPostArray["status"] 			= 1;
				$userObj->addUserPostJobByValue($userPostArray);
				
				
				// send email message
				$email_message = $emailObj->getEmailById(7);
				$e_subject = $email_message['email_subject'];
				$message = $email_message['email_content']; 
				
				$subject = str_replace('[SITENAME]', SITENAME, $e_subject);		 
				
				$message = str_replace('[NAME]', trim($_GET['fname'])." ".trim($_GET['lname']) , $message);
				$message = str_replace('[SITENAME]', SITENAME, $message);
				$message = str_replace('[USERNAME]', trim($_GET['emailaddress']), $message);
				$message = str_replace('[PASSWORD]', trim($password), $message); 
				$message = str_replace('[PHONENUMBER]',trim($_GET['mobno']), $message);
				$message = str_replace('[EMAIL]',trim($_GET['emailaddress']), $message);
				
				$message = str_replace('[DATEADDED]',trim($userPostArray["date_added"]), $message);
				$message = str_replace('[JOBSKILL]',trim($getjobdetail['trade_category']), $message);
				$message = str_replace('[DESCRIPTION]',trim($_GET['describe_job']), $message);
				$message = str_replace('[BUDGET]',"$".trim($budgetdetail['budget_name']), $message);
				$message = str_replace('[POSTCODE]',trim($_GET['postcode']), $message);
				
				$message = str_replace('[SITE_LINK]', SITENAME, $message);
				
				$to= trim($_GET['emailaddress']); 
				$adminEmail = $adminObj->getAdminDetails();
				$from = $adminEmail;
				$fromname= SITENAME;
								 
				$emailTemplate = file_get_contents('emailtemplate.html');
				$template_msg = str_replace('[SITENAME]',SITENAME , $emailTemplate);
				$template_msg = str_replace('[LOGO]','<img src="'.SITE_URL.'/uploads/logo/'.$imageDetails.'">', $template_msg);
				$template_msg = str_replace('[MESSAGE]',$message,$template_msg); 
				//echo $template_msg; exit;
				if(trim($to) != '')
				@send_mail($to, $template_msg, $subject, $from, $fromname); 
				
				if($_GET['postcode']!='')
				{
					$getalluserworkinpostalcode = $userObj->getalluserworkinpostalcode($_GET['state_id'],$_GET['postcode'],$_GET['jobtype'],$_GET['trade']);	
					
					for($n=0;$n<count($getalluserworkinpostalcode);$n++)
					{
							// send email message to tradesman for post job
							$email_message = $emailObj->getEmailById(9);
							$e_subject = $email_message['email_subject'];
							$message = $email_message['email_content']; 
							
							$subject = str_replace('[SITENAME]', SITENAME, $e_subject);		 
							
							$message = str_replace('[TNAME]', trim($getalluserworkinpostalcode[$n]['first_name'])." ".trim($getalluserworkinpostalcode[$n]['last_name']) , $message);
							$message = str_replace('[NAME]', trim($_GET['fname'])." ".trim($_GET['lname']) , $message);
							$message = str_replace('[SITENAME]', SITENAME, $message);
							$message = str_replace('[USERNAME]', trim($_GET['fname'])." ".trim($_GET['lname']), $message);							
							$message = str_replace('[DATEADDED]',trim($userPostArray["date_added"]), $message);
							$message = str_replace('[TRADESKILL]',trim($gettradedetail['trade_category']), $message);
							$message = str_replace('[JOBSKILL]',trim($getjobdetail['trade_category']), $message);
							$message = str_replace('[JOBDESC]',trim($_GET['describe_job']), $message);
							//$activationLink = SITE_URL."/leads/".base64_encode($getalluserworkinpostalcode[$n]['id'])."/".base64_encode($jobID)."/buylead/";		 
							$activationLink = SITE_URL."/leads/".base64_encode($jobID)."/buylead/";		 
							$activationLink = '<a href="'.$activationLink.'">'.$activationLink.'</a>';		 
							$message = str_replace('[BUYTHISLEAD]',$activationLink, $message);
														
							$message = str_replace('[SITE_LINK]', SITENAME, $message);
							
							$to= trim($getalluserworkinpostalcode[$n]['email']); 
							$adminEmail = $adminObj->getAdminDetails();
							$from = $adminEmail;
							$fromname= SITENAME;
											 
							$emailTemplate = file_get_contents('emailtemplate.html');
							$template_msg = str_replace('[SITENAME]',SITENAME , $emailTemplate);
							$template_msg = str_replace('[LOGO]','<img src="'.SITE_URL.'/uploads/logo/'.$imageDetails.'">', $template_msg);
							$template_msg = str_replace('[MESSAGE]',$message,$template_msg); 
							//echo $template_msg; exit;
							if(trim($to) != '')
							@send_mail($to, $template_msg, $subject, $from, $fromname); 
					}
				}
				
				
				//user already exist, send an email to admin (26)from_email
				$email_message = $emailObj->getEmailById(26);
				$e_subject = $email_message['email_subject'];
				$message = $email_message['email_content']; 
				
				$subject = str_replace('[SITENAME]', SITENAME, $e_subject);		 
				$message = str_replace('[SITENAME]', SITENAME, $message);			
				$message = str_replace('[USERNAME]', trim($_GET['emailaddress']), $message);
				
				
				$message = str_replace('[PHONENUMBER]',trim($_GET['mobno']), $message);
				$message = str_replace('[EMAIL]',trim($_GET['emailaddress']), $message);
				
				$message = str_replace('[DATEADDED]',trim($userPostArray["date_added"]), $message);
				$message = str_replace('[JOB_TYPE]',trim($getjobdetail['trade_category']), $message);
				$message = str_replace('[JOB_DESCRIPTION]',trim($_GET['describe_job']), $message);
				$message = str_replace('[BUDGET]',"$".trim($budgetdetail['budget_name']), $message);
				$message = str_replace('[POSTAL_CODE]',trim($_GET['postcode']), $message);
				
				$message = str_replace('[SITE_LINK]', SITENAME, $message);
				
				$to= $email_message['from_email']; 
				$from = $email_message['from_email']; ;
				$fromname= SITENAME;
								 
				$emailTemplate = file_get_contents('emailtemplate.html');
				$template_msg = str_replace('[SITENAME]',SITENAME , $emailTemplate);
				$template_msg = str_replace('[LOGO]','<img src="'.SITE_URL.'/uploads/logo/'.$imageDetails.'">', $template_msg);
				$template_msg = str_replace('[MESSAGE]',$message,$template_msg); 
				
				//echo $template_msg; exit;
				if(trim($to) != '')
				@send_mail($to, $template_msg, $subject, $from, $fromname); 
				
				 $_SESSION['getbids_id']  = $userId;			 
				 $_SESSION['first_name']      = $_GET['fname'];
				 $_SESSION['last_name']       = $_GET['lname'];	 				 
				 $_SESSION['user_type']       = 3;
				 $_SESSION['email']           = $_GET['emailaddress'];
				//$_SESSION['msg'] = "<div class='success_msg'><span>You have sucessfully post job!</span></div>";
				//redirect_to_link(SITE_URL.'/register/registersuccess/');
				
		}
}
else
{
	if($getuserdetail['user_type'] == 4)	
		$_SESSION['error'] ='This email address already used for tradesman user!';		
	else
		$_SESSION['error'] ='This email address already used for sub admin user!';			
}


unset($smarty);
?>